<template>
  <div>
    <!--  -->
    <div v-if="typeShow == 'dept'">
      <treeselect
        v-model="myDeptId"
        @input="input"
        :options="deptOptions"
        :disabled="disabled"
        noResultsText="暂无数据"
        :clearable="clearable"
        :normalizer="normalizer"
        placeholder="选择所属部门"
        class="treeselect-main"
        style="width: 100%; height: 32px"
      />
    </div>

    <div v-if="typeShow == 'duty'">
      <el-select
        :readonly="disabled"
        :disabled="disabled"
        filterable
        v-model="mydutyId"
        placeholder="请选择"
        clearable
        @clear="clear"
      >
        <el-option
          v-for="(item, index) in listUser"
          :key="index"
          :label="item.nickName"
          :value="item.userId"
        />
      </el-select>
    </div>
  </div>
</template>



<script>
import { listUser } from "@/api/system/user.js";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listDept } from "@/api/system/dept";

export default {
  data() {
    return {
      listUser: [], //用户列表
      deptOptions: [], //部门列表
      myDeptId: null,
      mydutyId: null,
    };
  },

  watch: {
    // 操作部门发生变化，清空人员input的回显值 重新请求人员列表
    deptId: {
      handler(val) {
        this.myDeptId = val;
        this.mydutyId = null;
        this.getlistUser(val);
        this.$emit("deptChange", val);
      },
      immediate:true
    },

    // 父组件传回人员id 回显
    dutyId: {
      handler(val) {
        this.mydutyId = val;
        // this.$emit('dutyChange',val);
      },
      immediate:true
    },
    mydutyId: {
      handler(val) {
        this.$emit("dutyChange", val);//返回用户id
        let userInfo =  this.listUser.filter(item=>item.userId==val);
        this.$emit("deptDateChange", userInfo[0]); //返回用户所有信息
      },
    },
  },
  props: {
    // 父组件传回的人员id 用于回显
    dutyId: {
      type: String | Number,
      default: null,
    },

    // 父组件传回的部门id 用于回显 和编辑人员 如果想使用部门联动人员，在人员引入是需要传部门id
    deptId: {
      type: String | Number,
      default: null,
    },
    typeShow: {
      type: String,
      default: "dept", //dept显示部门下拉框 duty显示人员下拉框 如果想使用部门联动人员，在人员引入是需要传部门id
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    clearable: {
      type: Boolean,
      default: true,
    },
  },
  components: { Treeselect },
  created() {
    this.init();
    this.getlistUser();
  },
  methods: {
    // 可清空的单选模式下用户点击清空按钮时触发
    clear(){
      this.$emit("clear");
    },
    input(val) {
      this.mydutyId = null;
      this.getlistUser(val);
      this.$emit("deptChange", val);
    },
    getlistUser(id) {
      const params = { deptId: id };
      this.listUser = [];
      listUser(params).then((res) => {
        // this.listUser = res.rows
        res.rows.forEach((item, index) => {
          this.$set(this.listUser, index, item);
        });
      });
    },
    init() {
      listDept().then((response) => {
        this.deptOptions = this.handleTree(response.data, "deptId");
      });
    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
  },
};
</script>

<style>
</style>